// TOWN SCRIPT
//    Town 2: Proving Grounds - Second Level

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 2, 27 = balanced pre-sets
// 2, 28 = presets all gone
// 2, 29 = generate new monsters

begintownscript;

variables;

short i, j, x, y, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(1);

	// check monsters...
	run_scenario_script(16);
	if (get_flag(2, 29) > 0) {
		// generate some more...
		set_flag(2, 29, 0);
		print_str_color("Generating monsters...", 3);
		j = get_ran(1, 14, 24);
		i = 0;
		while (i < j) {
			run_scenario_script(28);
			if (get_flag(100, 0) > 0) {
				run_scenario_script(26);
				place_monster(get_flag(100, 1), get_flag(100, 2), get_flag(100, 0), 0);
			}
			i = i + 1;
		}
		run_scenario_script(37);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading up to the previous level.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb up");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder back up...
		move_to_new_town(1, 6, 23);
	}
break;

beginstate 11;
	// reset hallway poem flags...
	set_flag(2, 0, 0);
	set_flag(2, 5, 0);
	set_flag(2, 6, 0);
break;

beginstate 12;
	run_scenario_script(11);
break;

beginstate 13;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (x >= 40) {
		x = 5;
	}
	else {
		x = 42;
	}
	teleport_party(x, y, 1);
break;

beginstate 14;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (x >= 40) {
		x = 4;
	}
	else {
		x = 42;
	}
	teleport_party(x, y, 1);
break;

beginstate 15;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a funny looking door here.", 0);
	add_dialog_str(1, "Go through it?", 0);
	add_dialog_choice(0, "Stay here");
	add_dialog_choice(1, "Go through");
	choice = run_dialog(0);
	if (choice == 2) {
		play_sound(-58);
		teleport_party(35, 6, 1);
		play_sound(59);
	}
break;

beginstate 16;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into the maze.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down...
		move_to_new_town(3, 33, 20);
	}
break;

beginstate 17;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-D), B exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				move_to_new_town(1, 25, 28);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				block_entry(0);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				move_to_new_town(3, 23, 26);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				move_to_new_town(4, 23, 26);
				end();
			}
		}
	}
break;

beginstate 18;
	if (get_flag(2, 1) == 0) {
		reset_dialog();
		add_dialog_str(0, "There is a statuette of a bear on a small pedestal.", 0);
		add_dialog_str(1, "Behind it is a sign reading, 'I've got a million of 'em'.", 0);
		add_dialog_choice(0, "Leave it");
		add_dialog_choice(1, "Take it");
		choice = run_dialog(0);
		if (choice == 2) {
			set_flag(2, 1, 1);
			change_spec_item(6, 1);
		}
	}
	else {
		message_dialog("You search the pedestal, but find nothing else.", "");
	}
break;

beginstate 19;
	if (get_flag(2, 2) == 0) {
		reset_dialog();
		add_dialog_str(0, "Upon a small silver disk rest a a statue of a frog wearing a red and blue cape.", 0);
		add_dialog_str(1, "Although made of metal, the statue inexplicably seems to come to life,", 0);
		add_dialog_str(2, "shaking its forelegs from side to side, accompanied by a high-pitched, 'Yeah!!!'", 0);
		add_dialog_str(3, "It then goes silent and unmoving again.", 0);
		add_dialog_choice(0, "Leave it");
		add_dialog_choice(1, "Take it");
		choice = run_dialog(0);
		if (choice == 2) {
			set_flag(2, 2, 1);
			change_spec_item(5, 1);
		}
	}
	else {
		message_dialog("You search the small silver disk, but find nothing else.", "");
	}
break;

beginstate 20;
	if (get_flag(2, 4) == 0) {
		reset_dialog();
		add_dialog_str(0, "Within the room is a statuette of a strange beast with the body of a chicken and the head of a cat.", 0);
		add_dialog_str(1, "The statue is made of bronze, and lies on a onyx pedestal.", 0);
		add_dialog_str(2, "There are unusual runes on a plaque thereon.", 0);
		add_dialog_str(3, "There is also a gold key laying here.", 0);
		add_dialog_choice(0, "Leave it");
		add_dialog_choice(1, "Take it");
		choice = run_dialog(1);
		if (choice == 2) {
			set_flag(2, 4, 1);
			change_spec_item(4, 1);
		}
	}
	else {
		message_dialog("You open and search the chest, but find nothing else.", "");
	}
break;

beginstate 21;
	if (has_special_item(2) == 0) {
		block_entry(1);
		reset_dialog();
		add_dialog_str(0, "As the party enters this room, a bronze-colored smoke fills it.", 0);
		add_dialog_str(1, "Suddenly, the party feels compelled to leave the room the way they came.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(1);
	}
	else {
		block_entry(0);
	}
break;

beginstate 22;
	if (has_special_item(3) == 0) {
		block_entry(1);
		reset_dialog();
		add_dialog_str(0, "As the party enters this room, a silvery fog descends from the ceiling.", 0);
		add_dialog_str(1, "Suddenly, images of terrible demons seem to form all around them.", 0);
		add_dialog_str(2, "The party members flee the room in terror.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(1);
	}
	else {
		block_entry(0);
	}
break;

// hallway poem states (23-25)
beginstate 23;
	if (get_flag(2, 0) == 0) {
		set_flag(2, 0, 1);
		print_str_color("A dungeon's dark...", 2);
	}
break;

beginstate 24;
	if (get_flag(2, 5) == 0) {
		set_flag(2, 5, 1);
		print_str_color("When it's not lit...", 2);
	}
break;

beginstate 25;
	if (get_flag(2, 6) == 0) {
		set_flag(2, 6, 1);
		print_str_color("Watch out or you'll step into a...", 2);
	}
break;
